package cn.iocoder.yudao.module.blog.service.ask;

import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.ask.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.ask.BlogAskDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.ask.BlogAskConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.ask.BlogAskMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 学员提问 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogAskServiceImpl implements BlogAskService {

    @Resource
    private BlogAskMapper askMapper;

    @Resource
    private MemberUserApi memberUserApi;

    @Override
    public Long createAsk(BlogAskCreateReqVO createReqVO) {
        // 插入
        BlogAskDO ask = BlogAskConvert.INSTANCE.convert(createReqVO);

        Long id = getLoginUserId();
        if(id == null){
            throw exception(ASK_USER_NOT_LOGIN);
        }

        MemberUserRespDTO userRespDTO = memberUserApi.getUser(id);
        if(userRespDTO == null){
            throw exception(ASK_USER_NOT_FOUND);
        }

        ask.setAvatar(userRespDTO.getAvatar());
        ask.setMemberId(id);
        ask.setNickname(userRespDTO.getNickname());
        ask.setMobile(userRespDTO.getMobile());

        ask.setReadNum(0);
        ask.setLikeNum(0);
        ask.setIsTop(false);

        askMapper.insert(ask);
        // 返回
        return ask.getId();
    }

    @Override
    public void updateAsk(BlogAskUpdateReqVO updateReqVO) {
        // 校验存在
        validateAskExists(updateReqVO.getId());
        // 更新
        BlogAskDO updateObj = BlogAskConvert.INSTANCE.convert(updateReqVO);
        askMapper.updateById(updateObj);
    }

    @Override
    public void deleteAsk(Long id) {
        // 校验存在
        validateAskExists(id);
        // 删除
        askMapper.deleteById(id);
    }

    private void validateAskExists(Long id) {
        if (askMapper.selectById(id) == null) {
            throw exception(ASK_NOT_EXISTS);
        }
    }

    @Override
    public BlogAskDO getAsk(Long id) {
        BlogAskDO askDO = askMapper.selectById(id);
        return askDO;
    }

    @Override
    public void readAsk(BlogAskDO askDO){
        if(askDO != null){
            askDO.setReadNum( askDO.getReadNum() + 1);
            askMapper.updateById(askDO);
        }
    }

    @Override
    public void likeAsk(Long askId){
        BlogAskDO askDO = askMapper.selectById(askId);
        if(askDO == null){
            return;
        }

        askDO.setLikeNum(askDO.getLikeNum() + 1);

        askMapper.updateById(askDO);
    }

    @Override
    public List<BlogAskDO> getAskList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return askMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogAskDO> getAskPage(BlogAskPageReqVO pageReqVO) {
        return askMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogAskDO> getAskList(BlogAskExportReqVO exportReqVO) {
        return askMapper.selectList(exportReqVO);
    }

}
